package color.koitq.picker.activty;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.rxjava.rxlife.RxLife;

import butterknife.BindView;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.ResourceObserver;
import rxhttp.wrapper.param.RxHttp;
import color.koitq.picker.BuildConfig;
import color.koitq.picker.R;
import color.koitq.picker.base.BaseActivity;
import color.koitq.picker.entity.ApiConfig;
import color.koitq.picker.entity.ChannelIdConfig;
import color.koitq.picker.entity.PolicyInfoModel;
import color.koitq.picker.entity.PolicyModel;

public class PrivacyActivity extends BaseActivity {

    public static final int ShowPolicy = 0;
    public static final int ShowUserAgreement = 1;

    public static void showRule(Context context, int type) {
        Intent intent = new Intent(context, PrivacyActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.topbar)
    QMUITopBarLayout topBar;

    @Override
    protected void init() {
        int type = getIntent().getIntExtra("type", ShowPolicy);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                webView.evaluateJavascript("setCompany('" + getString(R.string.companyname) + "')", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String s) {

                    }
                });
                webView.evaluateJavascript("textModify('" + getString(R.string.app_name) + "')", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String s) {

                    }
                });
                if ("vivo".equals(getString(R.string.channel))) {
                    webView.evaluateJavascript("setNotice('为向您提供更便携、更准确的信息展示、搜索及推送服务，我们会根据您的设备信息和您的浏览使用信息，提取您的偏好特征，并基于特征标签产出间接人群画像，用于展示、推送信息和可能的商业广告。穿山甲有可能会启用定向推送向用户推送广告，用户可以在我的-个性化推荐-关闭个性化推荐。')", new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String s) {

                        }
                    });
                }
            }
        });

        if (type == ShowPolicy) {
            topBar.setTitle("隐私政策");
            getPolicyInfo();
        } else {
            topBar.setTitle("用户协议");
            webView.loadUrl("file:///android_asset/user_agreement.html");
        }
        topBar.addLeftBackImageButton().setOnClickListener(v -> finish());
    }

    @Override
    protected int getContentViewId() {
        return R.layout.privacy_ui;
    }

    private void getPolicyInfo() {
        showLoading("加载中...");
        RxHttp.postForm(ApiConfig.getPolicyInfo)
                .add("umengid", BuildConfig.APPLICATION_ID)
                .add("channelId", ChannelIdConfig.getValue())
                .asClass(PolicyInfoModel.class)
                .to(RxLife.toMain(this))  //感知生命周期，并在主线程回调
                .subscribe(new ResourceObserver<PolicyInfoModel>() {
                    @Override
                    public void onNext(@NonNull PolicyInfoModel model) {
                        System.out.println("onNext: " + model);
                        if (model.getCode() == 200) {
                            webView.loadUrl(ApiConfig.baseUrl + model.getObj().getPageUrl());
                        } else {
                            webView.loadUrl("file:///android_asset/privacy_policy.html");
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        System.out.println("onError: " + e.getMessage());
                        webView.loadUrl("file:///android_asset/privacy_policy.html");
                    }

                    @Override
                    public void onComplete() {
                        System.out.println("onComplete");
                        hideLoading();
                    }
                });
    }

}
